package com.ruoyi.assessment.controller;

import com.github.pagehelper.Page;
import com.ruoyi.assessment.entity.AssessmentVo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.company.domain.XlkjCompany;
import com.ruoyi.company.service.IXlkjCompanyService;
import com.ruoyi.house.domain.XlkjHouse;
import com.ruoyi.house.service.IXlkjHouseService;
import com.ruoyi.incident.domain.XlkjIncident;
import com.ruoyi.incident.service.IXlkjIncidentService;
import com.ruoyi.ini.domain.XlkjIni;
import com.ruoyi.ini.service.IXlkjIniService;
import com.ruoyi.place.domain.XlkjPlace;
import com.ruoyi.place.service.IXlkjPlaceService;
import com.ruoyi.population.domain.XlkjPopulation;
import com.ruoyi.population.service.IXlkjPopulationService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 网格员绩效考核
 */
@RestController
@RequestMapping("/assessment/assessment")
public class AssessmentController extends BaseController {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IXlkjIniService iniService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    @GetMapping("/list")
    public TableDataInfo list() {
//        List<AssessmentVo> assessmentVoList = new ArrayList<>();
//        //查询所有网格员
//        startPage();
//        List<SysUser> sysUsers = sysUserService.selectUserListByRoleId(101L);
        startPage();
        Page<SysUser> sysUsers = (Page<SysUser>) sysUserService.selectUserListByRoleId(101L);
        Page<AssessmentVo> assessmentVoList = new Page<>();
        assessmentVoList.setTotal(sysUsers.getTotal());
        for (SysUser sysUser : sysUsers) {
            AssessmentVo assessmentVo = new AssessmentVo();
            //头像路径拼接
            if (sysUser.getAvatar() != null && sysUser.getAvatar().length() != 0) {
                sysUser.setAvatar(UrlUtils.getUrl() + sysUser.getAvatar());
            }
            assessmentVo.setSysUser(sysUser);
            //设置总分值
            assessmentVo.setSource(100);
            /**
             * 计算得分
             *    基础分值：50
             *          如果： 该网格员上报数 > 该部门总上报事件数 / 该部门人数         + 50分
             *          否者:  该部门总上报事件数 / 该部门人数 * 50
             *    加分项分值：50
             *          完成总数 / 上报总数  * 50
             */
            Integer source = 0;
            Long deptId = sysUser.getDeptId();
            SysDept sysDept = sysDeptService.selectDeptById(deptId);
            List<String> list = sysDeptMapper.selectIdByParentId(sysDept.getParentId());
            //设置该部门总人数
            assessmentVo.setUserNum(list.size());
            String str = "(";
            for (int i = 0; i < list.size(); i++) {
                str += list.get(i);
                if (i != list.size() - 1) {
                    str += ",";
                }
            }
            str += ")";
            XlkjIni ini = new XlkjIni();
            ini.setDepartmentList(str);
            List<XlkjIni> xlkjInis = iniService.selectXlkjIniList(ini);
            //设置该部门总上报事件数
            assessmentVo.setIncidentNum(xlkjInis.size());
            ini.setUserId(sysUser.getUserId());
            List<XlkjIni> xlkjInis1 = iniService.selectXlkjIniList(ini);
            //设置我的上报数
            assessmentVo.setIniNum(xlkjInis1.size());
            ini.setStatus("2");
            List<XlkjIni> xlkjInis2 = iniService.selectXlkjIniList(ini);
            //设置我的完成数
            assessmentVo.setIncidentSucc(xlkjInis2.size());
            //计算基础分
            int pj = assessmentVo.getIncidentNum() / assessmentVo.getUserNum();
            if (assessmentVo.getIniNum() >= pj) {
                source += 50;
            } else {
                source += assessmentVo.getIniNum() / pj * 50;
            }
            //计算加分项
            int fl;
            Integer incidentNum = assessmentVo.getIncidentNum();
            if (incidentNum == 0) {
                fl = 0;
            } else {
                fl = assessmentVo.getIncidentSucc() / assessmentVo.getIncidentNum();
            }
            source += fl * 50;
//            if(fl < 0.2){
//
//            }else if(fl >= 0.2 && fl < 0.5){
//                source += 20;
//            }else if(fl < 0.8 && fl >= 0.5){
//                source += 30;
//            }else {
//                source += 50;
//            }
            //设置总得分
            assessmentVo.setSourceNum(source);
            assessmentVoList.add(assessmentVo);
        }
        return getDataTable(assessmentVoList);
    }

}
